package com.motus.pm.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.motus.pm.dao.EmpInfoDao;
import com.motus.pm.vo.MotusEmpInfo;

@Repository
public class EmpInfoDaoImpl extends BaseDaoImpl<MotusEmpInfo> implements EmpInfoDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MotusEmpInfo> getEmpInfosByDeptId(int deptId) {
		Session session=sessionFactory.getCurrentSession();
		String hql="from MotusEmpInfo where motusDept.deptid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0, deptId);
		return query.list();
	}

	@Override
	public MotusEmpInfo getEmpByName(String name) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String hql = "from MotusEmpInfo where name =?";
		Query query = session.createQuery(hql);
		query.setString(0, name);
		if(query.list().size() > 0){
			return (MotusEmpInfo) query.list().get(0);
		}
		return null;
	}

	@Override
	public int getDeptIdByUserId(int userId) {
		int deptId=-1;
		Session session = sessionFactory.getCurrentSession();
		String hql = "select motusDept.deptid from MotusEmpInfo where id =?";
		Query query = session.createQuery(hql);
		query.setInteger(0, userId);
		if(query.list().size() > 0){
			deptId= (Integer) query.list().get(0);
		}
		return deptId ;
	}
	

}
